<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <title>书籍列表</title>
    <!-- 引入Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/animate.css@4.1.1/animate.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #e0e7ff 0%, #f8fafc 100%);
            min-height: 100vh;
        }
        .sidebar {
            height: 100vh;
            background: #343a40;
            color: #fff;
            padding-top: 60px;
            position: fixed;
            left: 0;
            top: 0;
            width: 200px;
        }
        .sidebar a {
            color: #fff;
            display: block;
            padding: 12px 24px;
            text-decoration: none;
            border-radius: 6px;
            transition: background 0.2s, transform 0.2s;
        }
        .sidebar a:hover {
            background: #6366f1;
            transform: scale(1.06);
        }
        .main-content {
            margin-left: 220px;
            padding: 90px 40px 30px 40px; /* 顶部padding加大，防止标题被遮住 */
            min-height: 100vh;
            animation: fadeInDown 0.8s;
        }
        @keyframes fadeInDown {
            from { opacity: 0; transform: translateY(-30px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .table th, .table td {
            vertical-align: middle;
        }
        .action-cell a {
            margin-right: 10px;
            transition: box-shadow 0.2s, transform 0.2s;
        }
        .action-cell a:hover {
            box-shadow: 0 2px 8px #6366f1aa;
            transform: scale(1.08);
        }
        .search-form {
            margin-bottom: 20px;
            animation: fadeIn 1.2s;
        }
        .pagination {
            justify-content: center;
        }
        .card {
            border-radius: 16px;
            box-shadow: 0 4px 24px rgba(99,102,241,0.08);
        }
        h2.mb-4 {
            font-weight: bold;
            letter-spacing: 2px;
            color: #6366f1;
            text-shadow: 0 2px 8px #6366f144;
            animation: fadeInDown 1s;
        }
        .navbar {
            background: linear-gradient(90deg, #6366f1 0%, #343a40 100%) !important;
            box-shadow: 0 2px 12px rgba(99,102,241,0.08);
        }
        .navbar-brand {
            font-weight: bold;
            letter-spacing: 1px;
        }
    </style>
    <!-- 引入Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        window.onload = function() {
            // 检查操作结果
            var addSuccess = "${sessionScope.addSuccess}";
            if (addSuccess === "true") {
                alert("添加成功！");
                <% session.removeAttribute("addSuccess"); %>
            }

            var updateSuccess = "${sessionScope.updateSuccess}";
            if (updateSuccess === "true") {
                alert("更新成功！");
                <% session.removeAttribute("updateSuccess"); %>
            }

            var deleteSuccess = "${sessionScope.deleteSuccess}";
            if (deleteSuccess === "true") {
                alert("删除成功！");
                <% session.removeAttribute("deleteSuccess"); %>
            }
        };
    </script>
</head>
<body>
<!-- 顶部导航栏 -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
    <div class="container-fluid">
        <a class="navbar-brand" href="#">图书管理系统</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav ms-auto">
                <li class="nav-item">
                    <a class="nav-link" href="addBook.jsp">新增图书</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="logoutServlet">退出登录</a>
                </li>
            </ul>
        </div>
    </div>
</nav>
<!-- 侧边栏 -->
<div class="sidebar">
    <h4 class="text-center">菜单</h4>
    <a href="bookList">图书列表</a>
    <a href="addBook.jsp">新增图书</a>
    <a href="menu.jsp">功能菜单</a>
    <a href="about.jsp">关于系统</a>
    <a href="help.jsp">帮助中心</a>
    <a href="contact.jsp">联系我们</a>
    <a href="changePassword.jsp">修改密码</a>
    <!-- 可扩展更多菜单项 -->
</div>
<!-- 主体内容 -->
<div class="main-content">
    <h2 class="mb-4">图书列表</h2>
    <!-- 搜索表单 -->
    <form class="row g-3 search-form" method="get" action="bookList">
        <div class="col-md-4">
            <input type="text" class="form-control" name="searchName" placeholder="按书名搜索" value="${param.searchName}">
        </div>
        <div class="col-md-4">
            <input type="text" class="form-control" name="searchAuthor" placeholder="按作者搜索" value="${param.searchAuthor}">
        </div>
        <div class="col-md-2">
            <button type="submit" class="btn btn-primary w-100">搜索</button>
        </div>
        <div class="col-md-2">
            <a href="bookList" class="btn btn-secondary w-100">重置</a>
        </div>
    </form>
    <!-- 图书表格 -->
    <div class="table-responsive">
        <table class="table table-striped table-hover align-middle">
            <thead class="table-dark">
            <tr>
                <th>序号</th>
                <th>书名</th>
                <th>作者</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <c:if test="${empty books}">
                <tr>
                    <td colspan="4" class="text-center">暂无数据</td>
                </tr>
            </c:if>
            <c:forEach items="${books}" var="book" varStatus="status">
                <tr>
                    <td>${status.index + 1 + (pageNum-1)*pageSize}</td>
                    <td>${book.name}</td>
                    <td>${book.author}</td>
                    <td class="action-cell">
                        <a href="bookDetailServlet?id=${book.id}" class="btn btn-sm btn-outline-info">查看详情</a>
                        <a href="updateBook.jsp?id=${book.id}&name=${book.name}&author=${book.author}" class="btn btn-sm btn-outline-primary">修改</a>
                        <a href="deleteByIdServlet?id=${book.id}" class="btn btn-sm btn-outline-danger" onclick="return confirm('确定要删除《${book.name}》吗？')">删除</a>
                    </td>
                </tr>
            </c:forEach>
            </tbody>
        </table>
    </div>
    <!-- 分页控件 -->
    <nav aria-label="Page navigation">
        <ul class="pagination">
            <c:choose>
                <c:when test="${pageNum == 1}">
                    <li class="page-item disabled">
                        <a class="page-link" href="#">上一页</a>
                    </li>
                </c:when>
                <c:otherwise>
                    <li class="page-item">
                        <a class="page-link" href="bookList?pageNum=${pageNum-1 < 1 ? 1 : pageNum-1}&searchName=${param.searchName}&searchAuthor=${param.searchAuthor}">上一页</a>
                    </li>
                </c:otherwise>
            </c:choose>
            <c:forEach begin="1" end="${totalPages}" var="i">
                <c:choose>
                    <c:when test="${i == pageNum}">
                        <li class="page-item active">
                            <a class="page-link" href="#">${i}</a>
                        </li>
                    </c:when>
                    <c:otherwise>
                        <li class="page-item">
                            <a class="page-link" href="bookList?pageNum=${i}&searchName=${param.searchName}&searchAuthor=${param.searchAuthor}">${i}</a>
                        </li>
                    </c:otherwise>
                </c:choose>
            </c:forEach>
            <c:choose>
                <c:when test="${pageNum == totalPages}">
                    <li class="page-item disabled">
                        <a class="page-link" href="#">下一页</a>
                    </li>
                </c:when>
                <c:otherwise>
                    <li class="page-item">
                        <a class="page-link" href="bookList?pageNum=${pageNum+1 > totalPages ? totalPages : pageNum+1}&searchName=${param.searchName}&searchAuthor=${param.searchAuthor}">下一页</a>
                    </li>
                </c:otherwise>
            </c:choose>
        </ul>
    </nav>
</div>
</body>
</html>